package cn.ecut;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Select {
    public static void main( String[] args ) throws  Exception {
        // 注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver") ;
//        System.out.println("注册驱动成功");
        // 获取数据库连接
        Connection connection ;
        String url = "jdbc:mysql://localhost:3306/ecut?serverTimezone=Asia/Shanghai" ;
        String username = "root" ;
        String password = "root" ;
        connection = DriverManager.getConnection(url , username , password) ;
//        System.out.println( connection );
//        System.out.println( "获取连接成功" );
        // 创建 Statement 对象
        Statement statement = connection.createStatement() ;
        // 执行SQL
        String SQL = "select * from country where id<3" ;
        boolean result = statement.execute( SQL ) ;
        System.out.println( result );
        // 处理结果
        ResultSet resultSet = statement.getResultSet();
        while( resultSet.next() ){
            int id = resultSet.getInt("id");
            String countryname = resultSet.getString("countryname");
            String countrycode = resultSet.getString("countrycode");
            System.out.println(id + " " + countryname + " " + countrycode );
        }
        // 释放资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}
